// index.js
const { Toast, Dialog } = require("../../config/modal")
const utils = require("../../utils/util")
const config = require("../../config/config")
const { LOGIN, LOCATION } = require("../../config/login.js")
// 获取应用实例
const App = getApp()
Page({
    data: {
        title: '去抖视频水印',
        offsetTop: App.globalData.window.navHeight,
        capsuleBarHeight: App.$getMenuButtonHeight(),
        isParse: true,
        author: "http://thisforyou.cn:9004/cbd-prod/mini/wechatQr.jpeg",
        test: "https://aweme.snssdk.com/aweme/v1/play/?video_id=v0300fg10000c4im2drc77u6qjgms3ig&ratio=720p&line=0",
        images:[{name:"抖音",src:"/static/img/logo-douyin.png"},{name:"快手",src:"/static/img/logo-kuaishou.png"},{name:"微视",src:"/static/img/logo-weishi.png"},{name:"火山",src:"/static/img/logo-huoshan.png"}],
        video: {
            src: '',
            desc: "视频文案"
        },
        value: "", //https://v.douyin.com/d8cCTmB/

        // 主题颜色
        theamTextColor: App.globalData.theamTextColor,

        // 通知条
        notice: "在代码阅读过程中人们说脏话的频率是衡量代码质量的唯一标准。",
    },
    page: {

    },
    URL: {
        douYin: "/parse/douyin",
    },

    showLagerImg(e) {
        let that=this;
        wx.previewImage({
            current: that.data.author+"?v="+new Date(), 
            urls: [that.data.author] 
        })
    },
    onLoad(options) {

    },
    reloadApp(){
        wx.clearStorage({
          success: (res) => {},
        })
    },
    onInput(e) {
        let _this = this
        wx.getClipboardData({
            success(res) {
                _this.setData({
                    value: e.detail.value
                });

            }
        })
    },
    copyDesc() {
        let that = this
        wx.setClipboardData({
            data: that.data.video.desc,
            success(res) {
                wx.getClipboardData({
                    success(res) {
                        console.log(res.data) // data
                    }
                })
            }
        })
    },
    copyData() {
        let that = this
        wx.getClipboardData({
            success(res) {
                that.setData({
                    value: res.data
                });
            }
        })
    },
    onSearch() {
        if (this.data.value == '' || this.data.value == undefined) {
            Toast("未检测到视频链接");
            return;
        }

        if (this.data.value.indexOf("http") < 0) {
            Toast("未检测到内容存在http视频链接");
            return;
        }

        App.$post(true, this.URL.douYin, { url: this.data.value }, res => {
            if (res.R) {
                this.setData({
                    'video.src': res.data.playApi,
                    'video.desc': res.data.desc,
                    isParse: true
                })
                return;
            }
        })
    },

    onDownVideo() {
        let that = this;

        if (this.data.video.src == '' || this.data.video.src == undefined) {
            Toast("未解析到视频");
            return;
        }

        let poster = this.data.video.src
        var aa = wx.getFileSystemManager();
        aa.writeFile({
            filePath: wx.env.USER_DATA_PATH + '/videodouyin.mp4',
            data: poster.slice(22),
            encoding: 'base64',
            success: res => {
                wx.saveVideoToPhotosAlbum({
                    filePath: wx.env.USER_DATA_PATH + '/videodouyin.mp4',
                    success: function (res) {
                        that.setData({
                            showQrCodeDialog: false
                        })
                        App.$alert('视频已保存到相册中')
                    },
                    fail: function (err) {
                        that.setData({
                            showQrCodeDialog: false
                        })
                        App.$alert('视频已保存失败')
                    }
                })
                console.log(res)
            }, fail: err => {
                that.setData({
                    showQrCodeDialog: false
                })
                App.$alert('视频已保存失败,请检查是否已经相册授权')
                console.log(err)
            }
        })
    },

    onShow() {

    },

    // tab栏切换
    onTypeChange(event) {


    },

    openSetting() {
        wx.openSetting({})
    },







    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    },



    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
    onShareAppMessage: function () {
        return App.$onShare('/pages/index/index', null, "抖音去水印")
    }

})
